package com.zlw.main.audioeffects.utils;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class StrUtils {
    /** 7位ASCII字符，也叫作ISO646-US、Unicode字符集的基本拉丁块      */
    public static final String US_ASCII = "US-ASCII";
    /** ISO拉丁字母表 No.1，也叫做ISO-LATIN-1     */
    public static final String ISO_8859_1 = "ISO-8859-1";
    /** 8 位 UCS 转换格式     */
    public static final String UTF_8 = "UTF-8";
    /** 16 位 UCS 转换格式，Big Endian(最低地址存放高位字节）字节顺序     */
    public static final String UTF_16BE = "UTF-16BE";
    /** 16 位 UCS 转换格式，Litter Endian（最高地址存放地位字节）字节顺序     */
    public static final String UTF_16LE = "UTF-16LE";
    /** 16 位 UCS 转换格式，字节顺序由可选的字节顺序标记来标识     */
    public static final String UTF_16 = "UTF-16";
    /** 中文超大字符集     **/
    public static final String GBK = "GBK";
    
    public static final String GB2312 = "GB2312";
    
    public static boolean compareStr(String s1, String s2) {
        boolean res = false;
        if (s1 == null && s2 == null)
            return true;
        if (s1 != null && s2 != null) {
            int t = s1.compareTo(s2);
            if (t == 0)
                res = true;
        }

        return res;
    }
    
    public static String dateToString(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(date);
    }
    
    public static Date stringToDate(String str) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date date = sdf.parse(str);
            return date;
        } catch(Exception e) {
            return null;
        }
    }
    
    public static boolean ISUTF8(byte[] s){
        int utf8ByteNumber=0;
        boolean ISALLASCII=true;
        int len = s.length;
    //  ALOGV("strlen=%d,string=%s",len,s);
        for (int i=0;i<len;i++){
            if((s[i]&0x80)==0x80){
                ISALLASCII=false;//the string is not all ASCII 
                if(utf8ByteNumber > 0){
                    if((s[i]&0xC0)!=0x80)
                        return false;
                    else
                        utf8ByteNumber--;
                }
                else{
                    if((s[i]&0xC0)==0x80)//the first utf byte cannot begin with 10
                        return false;
                    else{ 
                        if((s[i]&0xE0)==0xC0)//the first utf byte begin with 110
                            utf8ByteNumber=1;
                        else
                        if((s[i]&0xF0)==0xE0)//the first utf byte begin with 1110
                            utf8ByteNumber=2;
                        else
                        if((s[i]&0xF1)==0xF0)//the first utf byte begin with 11110
                            utf8ByteNumber=3;
                        else 
                            return false;//unicode :0~0x10FFFF
                    }
                }
            }
            else{
            if(utf8ByteNumber!=0)
                return false;
            }
        }
        return !ISALLASCII;
    }
    
    
    /**
     * 字符串编码转换的实现方法
     * @param str    待转换的字符串
     * @param oldCharset    源字符集
     * @param newCharset    目标字符集
     */
    public String changeCharset(String str, String oldCharset, String newCharset) throws UnsupportedEncodingException {
        if(str != null) {
            //用源字符编码解码字符串
            byte[] bs = str.getBytes(oldCharset);
            return new String(bs, newCharset);
        }
        return null;
    }
    
    public static String getEncoding(String str) {
        String encode = "GB2312";
        try {  
            if (str.equals(new String(str.getBytes(encode), encode))) {
                String s = encode;
                return s;  
            }  
        } catch (Exception exception) {
        }  
        encode = "ISO-8859-1";  
        try {  
            if (str.equals(new String(str.getBytes(encode), encode))) {
                String s1 = encode;
                return s1;  
            }  
        } catch (Exception exception1) {
        }  
        encode = "UTF-8";  
        try {  
            if (str.equals(new String(str.getBytes(encode), encode))) {
                String s2 = encode;
                return s2;  
            }  
        } catch (Exception exception2) {
        }  
        encode = "GBK";  
        try {  
            if (str.equals(new String(str.getBytes(encode), encode))) {
                String s3 = encode;
                return s3;  
            }  
        } catch (Exception exception3) {
        }  
        return "";  
    }  
    
    public static int strToInt(String s){
        int res =0;
        
        try {
            res = Integer.parseInt(s);
        } catch (Throwable e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        return res;
    }
    
    public static String getNoSpaceString(String before) {
        String result ="";
        if(before != null){
            result = before.replace(" ", "");
        }
        return result;
    }
}
